package com.zq.qq2.thread;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.zq.qq2.CodeUtils;
import com.zq.qq2.Constant;
import com.zq.qq2.MediaUtil;
import com.zq.qq2.R;
import com.zq.qq2.libsupersu.RootUtil;
import com.zq.qq2.service.FloatQQService;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * Created by xuebiao on 16/3/21.
 */
public class Login extends Thread {
    private Context context;
    private Handler handler;
    public Login(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }
    public void run() {
        Constant.Debug("登录开始检测....");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {

        }
        int neterror = 1;
        while( true ) {
            // 这里判断QQ文件加载
           if ( new File("/data/data/com.tencent.mobileqq/shared_prefs/").exists() )
               break;
            if ( neterror ++ > 30 ) {
                MediaUtil.Vibrater(context, R.raw.neterr, 10);
                Message msg = new Message();
                msg.what = 10;
                msg.obj = "网络异常";
                handler.sendMessage(msg);
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Constant.Debug("开始监测登录文件");
        final String QQLoginFile = "/data/data/com.tencent.mobileqq/shared_prefs/" + FloatQQService.QQAccount + ".xml";
        int errorCount = 1;
        while( !new File(QQLoginFile).exists() ) {
            Constant.Debug(String.format("开始第%d次输入验证码", errorCount));
            if ( errorCount >= 5 ) {
                Constant.Debug("失败多次了,请手动干预");
                MediaUtil.vibrater(context, 10);
                handler.sendEmptyMessage(6);
                return;
            }
           {
                Constant.Debug("开始识别验证码");
                new Thread(){
                    @Override
                    public void run() {
                        MediaUtil.Notify(context, R.raw.yzm);
                    }
                }.start();
                RootUtil.Get().BashExecute("screencap -p /mnt/sdcard/qq.png");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {

                }
                String filePath = "/mnt/sdcard/qq_code.png";
                try {
                    CodeUtils.cutPNG(
                            new FileInputStream("/mnt/sdcard/qq.png"),
                            new FileOutputStream(filePath),
                            140, 126, 250, 100, false);

                } catch (Exception e) {
                    Constant.Debug("截图失败:" + e.getMessage());
                }
                Constant.Debug("截图成功");
                String ret = CodeUtils.getCode(Constant.getSP(context).getInt("dama", 0) == 0);
                if ( !ret.equals("") ) {
                    RootUtil.Get().BashExecute("input text " + ret);
                    RootUtil.Get().BashExecute("input tap 440 70");

                    try {
                        int qc = 1;
                        while( !new File(QQLoginFile).exists() ) {
                            Constant.Debug("监测是否有登录成功文件");
                            Thread.sleep(1000);
                            if ( qc ++ > 10 )
                                break;
                        }
                    } catch (Exception e) {
                    }

                }
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {

            }
            errorCount ++;
        }
        Constant.Debug("识别完毕");
        handler.sendEmptyMessage(4);

    }
}
